// 清除用户缓存信息
export const cleanUserInfo = () => {
	// 清除缓存token
	uni.removeStorage({
		key: 'token',
		success: function (res) {
			
		}
	});
	// 清除缓存用户数据
	uni.removeStorage({
		key: 'userInfo',
		success: function (res) {
			
		}
	});
	// 清除缓存认证信息
	uni.removeStorage({
		key: 'userAuthInfo',
		success: function (res) {
			
		}
	});
	// 清除邀请码
	uni.removeStorage({
		key: 'inviteCode',
		success: function (res) {
			
		}
	});
}

import * as api from "@/api/api.js";
import request from "@/utils/request.js";
// 登录
export const login = async(username,password) => {
	return new Promise(async(resolve, reject) => {
		try{
			uni.showLoading({
				title: '正在登录...'
			});
			// let code = await getCode()
			// console.log()
			request({
				// url: api.weChatLogin,
				url: api.login,
				data: {
					// code: code
					// code: phoneNumber.code
					// username: '13412345678',
					// password: 'yanning'
					username,password
				},
				successCb: (res) => {
					console.log('登录成功',res);
					uni.hideLoading();
					uni.setStorage({
						key: 'token',
						data: res.data.token,
						success: () => {
							uni.showToast({
								title: "登录成功",
								icon: "none",
								duration: 2000,
							});
							uni.navigateBack();
							resolve(res)
						},
						fail(e) {
							reject(e)
						}
					});
				},
				failCb(e) {
					uni.hideLoading();
					uni.showToast({
						title: "登录失败",
						icon: "none",
						duration: 2000,
					});
					reject(e)
				}
			});
		}catch(e){
			//TODO handle the exception
			reject(e)
		}
	})
}
function getCode() {
	return new Promise(async(resolve, reject) => {
		uni.login({
			success(res) {
				if(res.code) {
					// console.log('获取code成功', res.code)
					resolve(res.code)
				} else {
					reject(res)
				}
			},
			fail(e) {
				reject(e)
			}
		})
	})
}
// 获取用户信息
export const getUserInfo = () => {
	return new Promise(async(resolve, reject) => {
		request({
			url: api.getUserInfo,
			method: 'GET',
			// data: {
			// 	code: this.code
			// },
			successCb: (res) => {
				console.log('获取成功',res)
				if(res) {
					uni.setStorage({
						key: 'userInfo',
						data: res,
						success: () => {
							resolve(res)
						},
						fail(e) {
							reject(e)
						}
					});
					let inviteUserId = uni.getStorageSync("inviteUserId")
					if(!res.isBindInviter && inviteUserId) {
						bindRelation()
					}
					let inviteCode = uni.getStorageSync("inviteCode")
					if(!inviteCode) {
						getInviteCode()
					}
				} else {
					reject(402)
				}
			},
			failCb: (e) => {
				uni.showToast({
					title: "获取用户信息失败",
					icon: "none",
					duration: 2000,
				});
				reject(e)
				// console.log('请求失败， ---', e)
			}
		})
	})
}

// 获取用户认证信息
export const getuserAuthInfo = () => {
	return new Promise(async(resolve, reject) => {
		request({
			url: api.userAuthInfo,
			method: 'GET',
			// data: {
			// 	code: this.code
			// },
			successCb: (res) => {
				// console.log('获取成功--==--',res)
				if(res) {
					uni.setStorage({
						key: 'userAuthInfo',
						data: res,
						success: () => {
							resolve(res)
						},
						fail(e) {
							reject(e)
						}
					});
				} else {
					reject('402')
				}
			},
			failCb: (e) => {
				uni.showToast({
					title: "获取用户认证信息失败",
					icon: "none",
					duration: 2000,
				});
				reject(e)
				// console.log('请求失败， ---', e)
			}
		})
	})
}
// 获取邀请码
export const getInviteCode = () => {
	return new Promise(async(resolve, reject) => {
		request({
			url: api.getInviteCode,
			method: 'GET',
			// data: {
			// 	code: this.code
			// },
			successCb: (res) => {
				console.log('获取获取邀请码成功--==--',res)
				if(res) {
					uni.setStorage({
						key: 'inviteCode',
						data: res.inviteCode,
						success: () => {
							resolve(res)
						},
						fail(e) {
							reject(e)
						}
					});
				} else {
					reject(res)
				}
			},
			failCb: (e) => {
				uni.showToast({
					title: "获取邀请码失败",
					icon: "none",
					duration: 2000,
				});
				reject(e)
				// console.log('请求失败， ---', e)
			}
		})
	})
}
// 绑定邀请用户关系
export const bindRelation = () => {
	return new Promise(async(resolve, reject) => {
		request({
			url: api.bindRelation,
			method: 'POST',
			data: {
				inviteCode: uni.getStorageSync("inviteUserId")
			},
			successCb: (res) => {
				console.log('绑定邀请用户关系成功--==--',res)
				if(res) {
					uni.removeStorage({
						key: 'inviteUserId',
						success: function (res) {
							
						}
					});
				} else {
					reject(res)
				}
			},
			failCb: (e) => {console.log(e)
				if (e.data.code === 500 && e.data.msg === '邀请码錯誤') {
					reject(e)
					uni.removeStorage({
						key: 'inviteUserId',
						success: function (res) {
							
						}
					});
				} else {
					uni.showToast({
						title: "绑定邀请用户关系失败",
						icon: "none",
						duration: 2000,
					});
					reject(e)
				}
				// console.log('请求失败， ---', e)
			}
		})
	})
}